package models;

import java.util.*;
import models.mappers.*;
import voidlib.*;


/**
 *
 * @author nevidimov
 */
public class Bandit extends VoidModel {
    protected VoidMapper mapper = new BanditMapper();
    
    @Override
    public VoidMapper getMapper() {
        return mapper;
    }
    
    @Override
    protected String _getModelId() {
        return "userId";
    }
    
    public int _userId;
    public int _hp;
    public int _clanId = -1;
    
    public Bandit() {
    }
    
    public Bandit(int userId, int hp, int clanId) {
        this._userId = userId;
        this._hp = hp;
        this._clanId = clanId;
    }
    
    public int getUserId() {
        return _userId;
    }
    
    public User getUser() {
        User bank = new User().getMapper().fetch(getUserId());
        return bank;
    }
    
    public int getHp() {
        return _hp;
    }
    
    public int getClanId() {
        return _clanId;
    }
    
    public Clan getClan() {
        if (getClanId() == -1) {
            return null;
        }
        return new Clan().getMapper().fetch(getClanId());
    }
    
    public boolean isClanOwner() {
        Clan clan = getClan();
        if (clan == null) {
            return false;
        }
        return clan.getOwnerId() == getUserId();
    }
    
    public void leaveClan() {
        Clan clan = getClan();
        if (clan == null) {
            return;
        }
        _clanId = -1;
        save();
        if (isClanOwner()) {
            clan.remove();
        }
        
    }
    
    public void inviteToMyClan(int banditId) {
        Invite invite = new Invite(_userId, banditId, new Date().getTime());
        invite.save();
    }
    
    public void joinClan(int clanId) {
        leaveClan();
        _clanId = clanId;
        save();
    }
    
    public int createClan(String title) {
        leaveClan();
        Clan clan = new Clan(_userId, title);
        int clanId = clan.save();
        joinClan(clanId);
        return clanId;
    }
}